import Vue from 'vue'
import Vuex from 'vuex'
// import axios from 'axios'

Vue.use(Vuex)
export default new Vuex.Store({
    state:{
        count:0,
        todoList:[],
        inputValue:'333',
        viewKey:'all'
    },
    mutations:{
        changeViewKey(state,key){
            state.viewKey = key;
        },
        add(state){
            state.count++;
        },
        addN(state,step){
            state.count += step;
        },
        sub(state){
            state.count--;
        },
        filterTodoList(state,filter){
            console.log('2222');
            if(filter){
                return state.todoList;
            }else{
                return state.todoList;
            }
        },
        deleteById(state,id){
            console.log(id);
            const index = state.todoList.findIndex(item=>{
                return item.id == id
            })
            if(index != -1){
                state.todoList.splice(index,1);
            }
        },
        setTodoList(state,data){
            state.todoList = data;
        },
        setInputValue(state,value){
            state.inputValue = value;
        },
        addItem(state,info){
            const obj = {
                id:'e6',
                info:info,
                status:false
            }
            state.todoList.push(obj)
        },
        changeStatusById(state,param){
            // state.todoList
            // console.log('1111111111111111111111',param);
            const index = state.todoList.findIndex(item=>{
                return item.id == param.id
            })
            // console.log(index);
            state.todoList[index].status = param.status;
            console.log(state.todoList);
        }
    },
    actions:{
        addAsync(context){
            setTimeout(() => {
                context.commit('addN',2)
            }, 1000);
        },
        getList(context){
            // axios.get('/list.json').then(({data})=>{
            //     console.log(data);
            //     context.commit('setTodoList',data)
            // })
            let data = [
                {
                    id:'a1',
                    info:'afefe',
                    status:true
                },
                {
                    id:'b2',
                    info:'23232',
                    status:false
                },
                {
                    id:'c3',
                    info:'33333',
                    status:true
                },
                {
                    id:'d4',
                    info:'33333',
                    status:true
                }
            ]
            context.commit('setTodoList',data)
        }
    },
    getters:{
        showCount(state){
            return '当前最新的数量是['+state.count+']';
        },
        getTodoList(state){
            if(state.viewKey=='all'){
                return state.todoList;
            }
            if(state.viewKey=='undone'){
                const aa = state.todoList.filter(item=>{
                    return item.status == false;
                })
                console.log(aa);
                return aa;
            }
            if(state.viewKey=='done'){
                const aa = state.todoList.filter(item=>{
                    return item.status == true;
                })
                console.log(aa);
                return aa;
            }
        },
        getCount(state){
            // console.log('!!!!!!');
            const count = state.todoList.filter(item=>item.status==false).length;
            // console.log('====='+count);
            return count
        }
    }
})